package csv;


import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JFileChooser;

public class Export {

    // [一键导出]调用的方法
    public void exportToCsv会员卡() {
        JFileChooser jf = new JFileChooser();
        jf.setFileSelectionMode(JFileChooser.SAVE_DIALOG | JFileChooser.DIRECTORIES_ONLY);
        // 弹出具有自定义 approve 按钮的自定义文件选择器对话框。
        jf.showDialog(null, null);
        File file = jf.getSelectedFile();
        String path = file.getAbsolutePath();// 取得绝对路径

        // 取得当前时间，格式是yyyyMMddHHmmss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date_str = sdf.format(new Date());

        // 拼接导出文件的路径
        // 路径：绝对路径+"采购列表"+"yyyyMMddHHmmss"
        path = path + "\\会员卡" + date_str + ".csv";
        System.out.println("csv导出路径：" + path);

        // 获取数据
        Dao MainDB = new Dao();
        List<Object[]> payMainList = null;
        try {
            payMainList = MainDB.select会员卡();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 获取csv表头
        Object[] names = { "卡号", "姓名", "类型", "余额", "折扣", "状态" };

        File f = CSVUtil.createCSVFile(names, payMainList, path);
        if (f != null) {
            Message.getMsg("恭喜，文件导出完毕！", "提示消息");
        }

    }

    public void exportToCsv消费记录() {
        JFileChooser jf = new JFileChooser();
        jf.setFileSelectionMode(JFileChooser.SAVE_DIALOG
                | JFileChooser.DIRECTORIES_ONLY);
        // 弹出具有自定义 approve 按钮的自定义文件选择器对话框。
        jf.showDialog(null, null);
        File file = jf.getSelectedFile();
        String path = file.getAbsolutePath();// 取得绝对路径

        // 取得当前时间，格式是yyyyMMddHHmmss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date_str = sdf.format(new Date());

        // 拼接导出文件的路径
        // 路径：绝对路径+"采购列表"+"yyyyMMddHHmmss"
        path = path + "\\消费记录" + date_str + ".csv";
        System.out.println("csv导出路径：" + path);

        // 获取数据
        Dao MainDB = new Dao();
        List<Object[]> payMainList = null;
        try {
            payMainList = MainDB.select消费记录();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 获取csv表头
        Object[] names = { "卡号", "姓名", "消费时间", "消费金额", "存款金额", "结存余额" };

        File f = CSVUtil.createCSVFile(names, payMainList, path);
        if (f != null) {
            Message.getMsg("恭喜，文件导出完毕！", "提示消息");
        }

    }


    public static void main(String[] args) {
        Export ex=new Export();
        ex.exportToCsv消费记录();
    }


}

